package com.csap.framework.integration.rocketmq.example.service;

import com.aliyun.openservices.ons.api.OnExceptionContext;
import com.aliyun.openservices.ons.api.SendCallback;
import com.aliyun.openservices.ons.api.SendResult;
import com.csap.framework.integration.producer.RocketmqProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yangchengfu
 * @description
 * @dataTime 2020年-10月-12日 13:30:00
 **/
@RestController
@RequestMapping
public class SendController {
    @Autowired(required = false)
    private RocketmqProducer producer;

    @GetMapping("send")
    public String send(@RequestParam(defaultValue = "Hello MQ") String c, @RequestParam(defaultValue = "") String topic, @RequestParam(defaultValue = "TagA") String tag) {
        producer.sendAsync(c, topic, tag, "ORDERID_100", new SendCallback() {
            @Override
            public void onSuccess(final SendResult sendResult) {
                // 消息发送成功。
                System.out.println("send message success. topic=" + sendResult.getTopic() + ", msgId=" + sendResult.getMessageId());
            }

            @Override
            public void onException(OnExceptionContext context) {
                // 消息发送失败，需要进行重试处理，可重新发送这条消息或持久化这条数据进行补偿处理。
                System.out.println("send message failed. topic=" + context.getTopic() + ", msgId=" + context.getMessageId());
            }
        });
        return "1";
    }
}
